package com.cskaoyan.proxy;

import com.cskaoyan.bean.JingTian;
import com.cskaoyan.bean.JingTianInterface;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author stone
 * @date 2021/12/24 15:05
 */
public class ExecuteJdkProxy {

    // 使用main方法，执行过程中会有一个工作路径
    public static void main(String[] args) {
        //通知应用程序把字节码保存起来，默认是不保存 → com.sun.proxy
        // ProxyGenerator
        System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        JingTian jingTian = new JingTian();
        JingTianInterface proxy = (JingTianInterface) Proxy.newProxyInstance(JingTian.class.getClassLoader(),
                JingTian.class.getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        method.invoke(jingTian, args);
                        System.out.println("一根烤肠和卤蛋");
                        return null;
                    }
                });
        proxy.buyBreakFast();
    }
}
